Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Code returns [4, 5, 6, 7, 8] instead of [4, 5, 7]. The loop doesn't acknowledge that [6] and [8] are in variable statues

function solution(statues) {
  statues.sort()
  let fullArray = [];
  let missingStatues = [];
  for (let i = Math.min(...statues); i <= Math.max(...statues); i++) {
    fullArray.push(i);
  }
  for (let i = 0; i < fullArray.length; i++) {
    if (!(fullArray[i] in statues)) {
      missingStatues.push(fullArray[i]);
    }
  }
  let missingStatuesCount = missingStatues.length;
  return missingStatues;
}

console.log(solution([6, 2, 3, 8]))

I was expecting [4, 5, 7] but I got [4, 5, 6, 7, 8]. The loop that pushes fullArray[i] into missingStatues, is also pushing [6] and [8], which are in statues and should not be pushed.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

function solution(statues) {
    statues.sort()

The default comparisons made by the .sort() method are string comparisons. Your arrays contain numbers, apparently, so you need a numeric comparator function:

    statues.sort((a, b) => a - b);

Then:

    for (let i=Math.min(...statues);i<=Math.max(...statues);i++) {
        fullArray.push(i);
    }

Once you have successfully sorted the array, statues[0] will be the smallest value and statues[statues.length - 1] will be the largest. There is no need to copy the array and call Math.min() or Math.max().

    for (let i=0;i<fullArray.length;i++) {
        if (!(fullArray[i] in statues)) {
            missingStatues.push(fullArray[i]);
        } 
    }

The in operator is for checking object keys, not values. What you're looking for is the .includes() method:

        if (!statues.includes(fullArray[i]))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda